package com.vader.boot.aspect;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Vader
 * @date 2022/5/25
 **/
@Slf4j
public class CglibProxyDemo {

    static final class Target implements JdkProxyDemo.Foo {

        @Override
        public void foo() {
            log.info("target_foo");
        }
    }

    public static void main(String[] args) {
        Target target = new Target();
        JdkProxyDemo.Foo before = (JdkProxyDemo.Foo) Enhancer.create(Target.class, (MethodInterceptor) (o, method, objects, methodProxy) -> {
            log.info("CglibProxyDemo_before");
            return methodProxy.invokeSuper(o, objects);
//            return method.invoke(target, objects);
        });
        before.foo();
    }
}
